package com.scuec.groupfive.mapper;

import com.scuec.groupfive.entity.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RollManagementMapper {
    /**
     * 根据年份和试卷类型获取题目，并按题目类型排序
     * @param examYear 年份
     * @param typeId 试卷类型ID
     * @return 题目列表
     */
    @Select("SELECT q.*, " +
            "cq.answer AS choice_answer, cq.analysis AS choice_analysis, cq.options, " +
            "fbq.answer_template, fbq.blanks, fbq.analysis AS fillblank_analysis, " +
            "sq.reference_answer, sq.answer_hints, sq.analysis AS subjective_analysis " +
            "FROM questions q " +
            "LEFT JOIN choice_questions cq ON q.question_id = cq.question_id " +
            "LEFT JOIN fill_blank_questions fbq ON q.question_id = fbq.question_id " +
            "LEFT JOIN subjective_questions sq ON q.question_id = sq.question_id " +
            "WHERE q.exam_year = #{examYear} AND q.type_id = #{typeId} " +
            "ORDER BY " +
            "CASE q.question_type " +
            "WHEN 'single_choice' THEN 1 " +
            "WHEN 'multiple_choice' THEN 2 " +
            "WHEN 'fill_in_the_blank' THEN 3 " +
            "WHEN 'essay' THEN 4 " +
            "END ASC")
    List<QuestionInfo> getQuestionsByYearAndType(@Param("examYear") Integer examYear, @Param("typeId") Integer typeId);

    /**
     * 更新题目基本信息
     * @param questionInfo 题目信息
     */
    @Update("UPDATE questions SET " +
            "content = #{content}, " +
            "difficulty = #{difficulty}, " +
            "keyword = #{keyword}, " +
            "chapter_id = #{chapterId} " +  // 只更新传入的字段
            "WHERE question_id = #{questionId}")
    void updateQuestion(QuestionInfo questionInfo);

    /**
     * 根据ID获取题目
     * @param questionId 题目ID
     * @return 题目信息
     */
    @Select("SELECT * FROM questions WHERE question_id = #{questionId}")
    QuestionInfo getQuestionById(@Param("questionId") Long questionId);

    /**
     * 更新选择题答案
     * @param choiceQuestion 选择题答案信息
     */
    @Update("UPDATE choice_questions SET " +
            "options = #{options}, " +
            "answer = #{answer}, " +
            "analysis = #{analysis} " +
            "WHERE question_id = #{questionId}")
    void updateChoiceAnswer(ChoiceQuestion choiceQuestion);

    /**
     * 根据题目ID获取选择题答案
     * @param questionId 题目ID
     * @return 选择题答案信息
     */
    @Select("SELECT * FROM choice_questions WHERE question_id = #{questionId}")
    ChoiceQuestion getChoiceAnswerByQuestionId(@Param("questionId") Long questionId);

    /**
     * 更新填空题答案
     * @param fillBlankQuestion 填空题答案信息
     */
    @Update("UPDATE fill_blank_questions SET " +
            "blanks = #{blanks}, " +
            "answer_template = #{answerTemplate}, " +
            "analysis = #{analysis} " +
            "WHERE question_id = #{questionId}")
    void updateFillBlankAnswer(FillBlankQuestion fillBlankQuestion);

    /**
     * 根据题目ID获取填空题答案
     * @param questionId 题目ID
     * @return 填空题答案信息
     */
    @Select("SELECT * FROM fill_blank_questions WHERE question_id = #{questionId}")
    FillBlankQuestion getFillBlankAnswerByQuestionId(@Param("questionId") Long questionId);

    /**
     * 更新主观题答案
     * @param subjectiveQuestion 主观题答案信息
     */
    @Update("UPDATE subjective_questions SET " +
            "reference_answer = #{referenceAnswer}, " +
            "answer_hints = #{answerHints}, " +
            "analysis = #{analysis} " +
            "WHERE question_id = #{questionId}")
    void updateSubjectiveAnswer(SubjectiveQuestion subjectiveQuestion);

    /**
     * 根据题目ID获取主观题答案
     * @param questionId 题目ID
     * @return 主观题答案信息
     */
    @Select("SELECT * FROM subjective_questions WHERE question_id = #{questionId}")
    SubjectiveQuestion getSubjectiveAnswerByQuestionId(@Param("questionId") Long questionId);
}